// 统一封装接口地址
import axios from '@/utils/request.js'
// import { getToken } from '../utils/token'

export const getAllChannelsAPI = () => {
  return axios({
    url: '/v1_0/channels',
    method: 'GET'
  })
}
// 更新频道
export const updateChannelsAPI = ({ channels }) => {
  return axios({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels
    }
  })
}

export const loginAPI = ({ mobile, code }) => {
  return axios({
    method: 'POST',
    url: '/v1_0/authorizations',
    data: {
      mobile, code
    }
  })
}

export const getUserChannelsAPI = () => {
  return axios({
    url: '/v1_0/user/channels'
    // headers: {
    //   Authorization: `Bearer ${getToken()}`
    // }
  })
}

export const getAllAriticleListAPI = ({ channel_id, timestamp }) => {
  return axios({
    url: '/v1_0/articles',
    // headers: {
    //   Authorization: `Bearer ${getToken()}`
    // },
    params: {
      channel_id, timestamp
    }
  })
}
// 不感兴趣
export const dislikeArticleAPI = ({ artId }) => {
  // console.log('aaa' + artId)

  axios({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    data: { target: artId }
    // headers: {
    //   Authorization: `Bearer ${getToken()}`
    // }

  })
}

export const articleReportsAPI = ({ artId, type }) => {
  console.log('aaaa' + artId, 'aaa' + type)
  axios({
    url: '/v1_0/article/reports',
    method: 'POST',
    // headers: {
    //   Authorization: `Bearer ${getToken()}`
    // },
    data: {
      target: artId,
      type: type
    }

  })
}

// 搜索
export const suggestListAPI = ({ keywords }) => {
  return axios({
    url: '/v1_0/suggestion',
    params: {
      q: keywords
    }
  })
}
// 搜索结果
export const searchResultAPI = ({ page = 1, per_page = 10, q }) => {
  return axios({
    url: '/v1_0/search',
    params: {
      page,
      per_page,
      q
    }
  })
}
// 文章页面
export const detailAPI = ({ artId }) => {
  return axios({
    url: `/v1_0/articles/${artId}`
  })
}
// 关注作者
export const userFollowedAPI = ({ aut_id }) => {
  return axios({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target: aut_id
    }
  })
}
// 取关
export const userUnFollowedAPI = ({ aut_id }) => {
  return axios({
    url: `/v1_0/user/followings/${aut_id}`,
    method: 'DELETE'
  })
}
// 点赞
export const likeArticleAPI = ({ artId }) => {
  // console.log(artId)
  return axios({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target: artId
    }
  })
}
// 取消点赞
export const unlikeArticleAPI = ({ artId }) => {
  return axios({
    url: `/v1_0/article/likings/${artId}`,
    method: 'DELETE'
  })
}
// 评论
// 对文章评论
export const commentListAPI = ({ art_id, offset = null, limit = 10 }) => {
  return axios({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source: art_id,
      offset,
      limit
    }
  })
}
// 评论点赞
export const commentLikingAPI = ({ com_id }) => {
  return axios({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: com_id
    }
  })
}
export const uncommentLikingAPI = ({ com_id }) => {
  return axios({
    url: `/v1_0/comment/likings/${com_id}`,
    method: 'DELETE'
  })
}
// 对文章评论
export const commentSendAPI = ({ target, content, art_id = null }) => {
  const obj = {
    target, content
  }
  if (art_id) obj.art_id = art_id
  return axios({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj
  })
}

// 用户自己信息页面
export const userInfoAPI = () => {
  return axios({
    url: '/v1_0/user'
  })
}
// 获取用户编辑页面信息
export const userProfileAPI = () => {
  return axios({
    url: '/v1_0/user/profile'
  })
}
// 个人中心—— 修改头像
export const updatePhotoAPI = ({ formObj }) => {
  return axios({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: formObj
    // 如果你的请求体内容是表单对象, 浏览器会自动携带请求头Content-Type为multipart/form-data
  })
}
// 用户-修改自身信息
export const updateUserProfileAPI = ({ birthday = undefined, name = undefined, gender = undefined, intro = undefined }) => {
  console.log(name)
  const obj = {}
  if (birthday) obj.birthday = birthday
  if (name) obj.name = name
  if (gender) obj.gender = gender
  if (intro) obj.intro = intro
  console.log(obj)
  return axios({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: { ...obj }
  })
}
// 用户-刷新token
export const getNewTokenAPI = () => {
  return axios({
    url: '/v1_0/authorizations',
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${localStorage.getItem('refresh_token')}`
    }
  })
}
